import * as THREE from "../assets/plugins/build/three.module.js";
import Stats from "../assets/plugins/map3d/jsm/libs/stats.module.js";
import { OrbitControls } from "../assets/plugins/map3d/jsm/controls/OrbitControls.js";
import { FlyControls } from "../assets/plugins/map3d/jsm/controls/FlyControls.js";

// import { EffectComposer } from "../assets/plugins/map3d/jsm/postprocessing/EffectComposer.js";
// import { RenderPass } from "../assets/plugins/map3d/jsm/postprocessing/RenderPass.js";
// import { FilmPass } from "../assets/plugins/map3d/jsm/postprocessing/FilmPass.js";

import {util} from '../assets/plugins/map3d/js/util';
import {meshHelper} from '../assets/plugins/map3d/js/meshHelper';
import {drawThreeGeo}  from '../assets/plugins/map3d/js/threeGeoJSON';  

window.earth={};
$(function(){
    const CONTAINER_ID = 'earth';

    function onWindowResize() {
        if(!earth.animateID){
          return;
        }
        let mapPanel = document.getElementById(CONTAINER_ID);
  
        earth.SCREEN_HEIGHT = mapPanel.clientHeight;
        earth.SCREEN_WIDTH = mapPanel.clientWidth;
  
        earth.camera.aspect = earth.SCREEN_WIDTH / earth.SCREEN_HEIGHT;
        earth.camera.updateProjectionMatrix();
  
        earth.renderer.setSize(earth.SCREEN_WIDTH, earth.SCREEN_HEIGHT);        
      }

    //初始化参数
    earth.mapPanel = document.getElementById(CONTAINER_ID);
    earth.panelHeight = earth.mapPanel.clientHeight;
    earth.panelWidth = earth.mapPanel.clientWidth;

    earth.radius = 50;//6371;
    earth.tilt = 0.41;
    earth.rotationSpeed = 0.02;
    earth.cloudsScale = 1.005;
    earth.moonScale = 0.23;
    earth.MARGIN = 0;
    earth.SCREEN_HEIGHT = earth.panelHeight - earth.MARGIN * 2;
    earth.SCREEN_WIDTH = earth.panelWidth;    

    earth.textureLoader = new THREE.TextureLoader();
    earth.clock = new THREE.Clock();

    //构造场景、相机
    earth.scene = new THREE.Scene();
    earth.scene.background = earth.textureLoader.load( require('../assets/plugins/map3d/textures/planets/bg.jpg') );
    earth.camera = new THREE.PerspectiveCamera(45, earth.SCREEN_WIDTH / earth.SCREEN_HEIGHT, 0.1, 1000);
    earth.camera.position.set(-31,113,80);
    earth.camera.up.set(0, 0, 1);
    earth.camera.lookAt(earth.scene.position);

    //构造地球体-背景
    var materialEarth = new THREE.MeshBasicMaterial({
        map: earth.textureLoader.load(
          require("../assets/plugins/map3d/textures/planets/earth_atmos_2048.jpg")
        ),
        transparent: true
    });  
    let geometry = new THREE.SphereGeometry(50, 42, 42);
    let meshEarth = new THREE.Mesh(geometry, materialEarth);
    meshEarth.rotation.x = 1.57;
    meshEarth.scale.set(earth.cloudsScale, earth.cloudsScale, earth.cloudsScale);
    earth.scene.add( meshEarth );
    earth.meshEarth = meshEarth;
 
    //构造渲染器
    earth.renderer = new THREE.WebGLRenderer({ antialias: true ,alpha: true});
    earth.renderer.setPixelRatio(window.devicePixelRatio);
    earth.renderer.setSize(earth.SCREEN_WIDTH, earth.SCREEN_HEIGHT);
    earth.mapPanel.appendChild(earth.renderer.domElement);
    
    //控制
    var controls = new OrbitControls(earth.camera, earth.renderer.domElement);
    controls.update();
    //性能统计
    earth.stats = new Stats();
    earth.mapPanel.appendChild(earth.stats.dom);

    // postprocessing
    // let renderModel = new RenderPass(earth.scene, earth.camera);
    // let effectFilm = new FilmPass(0.35, 0.75, 2048, false);
    // earth.composer = new EffectComposer(earth.renderer);
    // earth.composer.addPass(renderModel);
    // earth.composer.addPass(effectFilm);

    window.addEventListener("resize", onWindowResize, false);

    function animate() {
      earth.animateID = requestAnimationFrame(animate);
      render();
      earth.stats.update();
        //controls.update();
        //earth.renderer.render(earth.scene, earth.camera);
    }

    function render() {
      earth.renderer.render( earth.scene, earth.camera );


      // postprocessing
      // rotate the planet and clouds
      // var delta = earth.clock.getDelta();
      // //this.meshClouds.rotation.y += this.rotationSpeed * delta;
      // // meshClouds.rotation.y += 1.25 * earth.rotationSpeed * delta;
      // // this.earth.rotation.y += 1.25 * this.rotationSpeed * delta;

      // // slow down as we approach the surface
      // earth.dPlanet = earth.camera.position.length();
      // earth.composer.render(delta);
    }

    //添加世界区域    
    $.get('../assets/plugins/map3d/js/word.geojson').then(data=>{
      
      let worldMesh = meshHelper.createMeshWorld(data);

      //中国各省
      let china = require('../assets/plugins/map3d/js/china.json');
      const chinaData = util.decode(china);   
      meshHelper.createMeshChina(chinaData,worldMesh); 
      earth.scene.add(worldMesh);

      //加点测试
      let points= [{
        position: [120.267365,35.98903],
        title: 'Point Test'
      }];
      for(let [i, d] of points.entries()){
          let point = new THREE.Mesh(new THREE.BoxGeometry(0.5,0.5,0.5), new THREE.MeshBasicMaterial({
              color:0xff0000
          }));
          point.position.copy(util.lglt2xyz(...d.position, 50+0.5));
          //point.position.copy(new THREE.Vector3(-20.391510672687602,34.941561736016446,30.381517250370493));
          console.log(point.position);
          point.lookAt(new THREE.Vector3())
          point.name = i
          worldMesh.add(point); 
      }
    });
    
 

    //开始渲染
    render();    
    animate();
    
});
